import yaml
import os
from API_TEST.apis.wework import WeWork
from API_TEST.settings import CONFIG_DIR


class Department(WeWork):
    def __init__(self):
        self.get_access_token()

    def get_access_token(self):
        """ 获取access_token """
        url = "/gettoken"
        info = yaml.safe_load(open(os.path.join(CONFIG_DIR, "wework.yml")))
        params = {
            "corpid": info.get("corpid"),
            "corpsecret": info.get("corpsecret").get("department")
        }
        resp = self.send("GET", url, params=params)
        self.access_token = resp.json().get("access_token")

    def create(self, data):
        """
        创建部门
        """
        url = "/department/create"
        params = {"access_token": self.access_token}
        resp = self.send("POST", url, params=params, json=data)
        return resp

    def get_list(self):
        """
        获取部门列表
        """
        url = "/department/list"
        params = {"access_token": self.access_token}
        resp = self.send("GET", url, params=params)
        return resp

    def is_in_department_list(self, depart_id):
        """
        判断depart_id 是否在部门列表中
        """
        resp = self.get_list()
        if depart_id in self.jsonpath_list(resp.json(), "$..id"):
            return True
        else:
            return False
